package com.ruoyi.app.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.domain.*;
import com.ruoyi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app")
public class AppController extends BaseController {
    @Autowired
    private IJkArticleService articleService;

    @Autowired
    private IJkVideoService videoService;

    @Autowired
    private IJkBannerService bannerService;

    @Autowired
    private IJkGroupService groupService;

    @Autowired
    private IJkFllowupService fllowupService;

    @Autowired
    private IJkUserService userService;

    @Autowired
    private IJkEducationService educationService;

    @Autowired
    private IJkToolService toolService;

    @Autowired
    private IJkFllowupMessageService messageService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IJkPraiseRecordsService praiseRecordsService;

    @Autowired
    private IJkHospitalAreaService areaService;

    @Autowired
    private IJkVideoTypeService typeService;

    //文章列表
    @GetMapping("/getArticlelist")
    public TableDataInfo getArticlelist(JkArticle jkArticle)
    {
        startPage();
        List<JkArticle> list = articleService.selectJkArticleList(jkArticle);
        return getDataTable(list);
    }

    //视频列表
    @GetMapping("/getVideolist")
    public TableDataInfo getVideolist(JkVideo jkVideo)
    {
        startPage();
        List<JkVideo> list = videoService.selectJkVideoList(jkVideo);
        return getDataTable(list);
    }

    //轮播图列表
    @GetMapping("/getBannerlist")
    public AjaxResult getBannerlist(JkBanner jkBanner)
    {
        List<JkBanner> list = bannerService.selectJkBannerList(jkBanner);
        return AjaxResult.success().put("list",list);
    }

    //文章详情
    @GetMapping(value = "/getArticleDetail")
    public AjaxResult getArticleDetail(Long id)
    {
        JkArticle jkArticle = articleService.selectJkArticleById(id);
        Integer pv = setPv(id, jkArticle.getArticleType());
        jkArticle.setPvCount(pv);
        return success(jkArticle);
    }

    //康复视频详情
    @GetMapping(value = "/getVideoDetail")
    public AjaxResult getVideoDetail(Long id)
    {
        JkVideo jkVideo = videoService.selectJkVideoById(id);
        Integer pv = setPv(id, Constants.RESOURCE_KFDL);
        jkVideo.setPvCount(pv);
        return success(jkVideo);
    }

    //扫码加入用户组
    @PostMapping("/joinGroup")
    public AjaxResult joinGroup(@RequestBody JkGroupUser groupUser)
    {
        if(groupUser.getUserId()==null||groupUser.getGroupId()==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(groupService.joinGroup(groupUser));
    }

    //随访列表
    @GetMapping("/getFllowupList")
    public TableDataInfo getFllowupList(JkFllowup jkFllowup)
    {
        startPage();
        List<JkFllowup> jkFllowups = fllowupService.selectJkFllowupList(jkFllowup);
        return getDataTable(jkFllowups);
    }

    //提交随访
    @PostMapping("/submitFllowup")
    public AjaxResult submitFllowup(@RequestBody JkFllowup jkFllowup)
    {
        return toAjax(fllowupService.insertJkFllowup(jkFllowup));
    }

    //修改随访
    @PutMapping("/updateFllowup")
    public AjaxResult updateFllowup(@RequestBody JkFllowup jkFllowup)
    {
        return toAjax(fllowupService.updateJkFllowup(jkFllowup));
    }

    //随访详情
    @GetMapping(value = "/getFllowupDetail")
    public AjaxResult getFllowupDetail(Long id)
    {
        return success(fllowupService.selectJkFllowupById(id));
    }

    //修改用户-app绑定院区
    @PutMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody JkUser jkUser)
    {
        return toAjax(userService.updateJkUser(jkUser));
    }

    //宣教列表
    @GetMapping("/getEducationlist")
    public TableDataInfo getEducationlist(JkEducation education)
    {
        startPage();
        List<JkEducation> list = educationService.selectJkEducationList(education);
        return getDataTable(list);
    }

    //宣教详情
    @GetMapping(value = "/getEducationDetail")
    public AjaxResult getEducationDetail(Long id)
    {
        JkEducation jkEducation = educationService.selectJkEducationById(id);
        Integer pv = setPv(id, Constants.RESOURCE_XJ);
        jkEducation.setPvCount(pv);
        return success(jkEducation);
    }

    //康复用具列表
    @GetMapping("/getToollist")
    public TableDataInfo getToollist(JkTool jkTool)
    {
        startPage();
        List<JkTool> jkTools = toolService.selectJkToolList(jkTool);
        return getDataTable(jkTools);
    }

    //用具详情
    @GetMapping(value = "/getToolDetail")
    public AjaxResult getToolDetail(Long id)
    {
        JkTool jkTool = toolService.selectJkToolById(id);
        Integer pv = setPv(id, Constants.RESOURCE_KFYJ);
        jkTool.setPvCount(pv);
        return success(jkTool);
    }

    //消息列表
    @GetMapping("/getMessagelist")
    public TableDataInfo getMessagelist(JkFllowupMessage jkFllowupMessage)
    {
        startPage();
        List<JkFllowupMessage> jkFllowupMessages = messageService.selectJkFllowupMessageList(jkFllowupMessage);
        return getDataTable(jkFllowupMessages);
    }

    //未读消息数量
    @GetMapping("/getMessageCount")
    public AjaxResult getMessageCount(JkFllowupMessage jkFllowupMessage)
    {
        List<JkFllowupMessage> jkFllowupMessages = messageService.selectJkFllowupMessageList(jkFllowupMessage);
        return success(jkFllowupMessages.size());
    }

    //修改消息
    @PutMapping("/updateMessage")
    public AjaxResult updateMessage(@RequestBody JkFllowupMessage fllowupMessage)
    {
        return toAjax(messageService.updateJkFllowupMessage(fllowupMessage));
    }

    public Integer setPv(Long id, int type){
        String key="PV_"+type+"_"+id;
        Object countObj = redisCache.getCacheObject(key);
        Integer count = 0;
        if(countObj!=null){
            count = (Integer) countObj;
        }
        count+=1;
        redisCache.setCacheObject(key,count);
        return count;
    }

    //点赞
    @PostMapping("/addParise")
    public AjaxResult addParise(@RequestBody JkPraiseRecords praiseRecords)
    {
        praiseRecordsService.insertJkPraiseRecords(praiseRecords);
        return success().put("pariseId",praiseRecords.getId());
    }

    //取消点赞
    @DeleteMapping("/cancelParise/{id}")
    public AjaxResult cancelParise(@PathVariable Long id)
    {
        return toAjax(praiseRecordsService.deleteJkPraiseRecordsById(id));
    }

    //点赞详情
    @GetMapping("/getPariseDetail")
    public AjaxResult getPariseDetail(JkPraiseRecords jkPraiseRecords)
    {
        List<JkPraiseRecords> list = praiseRecordsService.selectJkPraiseRecordsList(jkPraiseRecords);
        return success().put("status",list.size()>0?1:0).put("pariseId",list.size()>0?list.get(0).getId():null);
    }

    //院区列表
    @GetMapping("/getAreaList")
    public AjaxResult getAreaList(JkHospitalArea jkHospitalArea)
    {
        List<JkHospitalArea> jkHospitalAreas = areaService.selectJkHospitalAreaList(jkHospitalArea);
        return success(jkHospitalAreas);
    }

    //视频分类列表
    @GetMapping("/getVideoTypeList")
    public AjaxResult getVideoTypeList(JkVideoType videoType)
    {
        List<JkVideoType> jkVideoTypes = typeService.selectJkVideoTypeList(videoType);
        return success(jkVideoTypes);
    }

    //同步pv 暂时先不写
//    public void SyncPv(){
//        String pattern = "PV_";
//        Set<String> allkeys = redisCache.getAllkeys(pattern);
//        Iterator<String> iterator = allkeys.iterator();
//        while (iterator.hasNext()){
//            String key = iterator.next();
//            Map<String, Object> cacheMap = redisCache.getCacheMap(key);
//            Object count = cacheMap.get("count");
//            String type = key.split("_")[2];
//            String id = key.split("_")[3];
//            if(type.equals(Constants.RESOURCE_JKKP)||type.equals(Constants.RESOURCE_YDYY)){
//                JkArticle jkArticle = new JkArticle();
//                jkArticle.setId(Long.valueOf(id));
//                jkArticle.setArticlePv((Integer) count);
//                articleService.updateJkArticle(jkArticle);
//            }
//        }
//    }
}
